package com.zhongjian.technologycal.mall.activity.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.adapter.MemberAreaAdaper;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.bean.TestAdapter;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/11/10.
 * 特色赠品页面实现类/众健商城
 */

public class MembersAreaTagActivity extends BaseActivity {
    @BindView(R.id.ll_line0)
    LinearLayout ll_line0;
    @BindView(R.id.ll_line1)
    LinearLayout ll_line1;
    @BindView(R.id.ll_line2)
    LinearLayout ll_line2;
    @BindView(R.id.ll_line3)
    LinearLayout ll_line3;
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.items_line0)
    View items_line0;
    @BindView(R.id.items_line1)
    View items_line1;
    @BindView(R.id.items_line2)
    View items_line2;
    @BindView(R.id.items_line3)
    View items_line3;
    @BindView(R.id.select_down_or_up_price)
    ImageView select_down_or_up_price;
    @BindView(R.id.select_up_or_down_elecNum)
    ImageView select_up_or_down_elecNum;
    @BindView(R.id.list_item_view)
    ListView list_item_view;
    @BindView(R.id.donation_smartrefreshlayout)
    RefreshLayout donation_smartrefreshlayout;

    private String tag;
    private int type = 1;
    private int page = 0;
    private int pageNum = 10;
    private List<TestAdapter> listItems = new ArrayList<>();
    private MemberAreaAdaper memberAreaAdaper;
    private String mTitle;
    private int state = 0;
    private boolean isUpOrDown = true;
    private boolean isUpOrDown1 = true;

    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTitle = getIntent().getStringExtra("mTitle");
        tv_main_title.setText(mTitle);
        tag = getIntent().getStringExtra("tag");
        initRequestMembersArea(1);
        /**
         * 刷新数据
         */
        donation_smartrefreshlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {

                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 0;
                        if(state == 0){
                            initRequestMembersArea(1);
                        }else if(state == 1){
                            if(isUpOrDown){
                                initRequestMembersArea(2);
                            }else{
                                initRequestMembersArea(3);
                            }
                        }else if(state == 2){
                            initRequestMembersArea(4);
                        }else if(state == 3){
                            if(isUpOrDown1){
                                initRequestMembersArea(5);
                            }else{
                                initRequestMembersArea(6);
                            }
                        }

                        donation_smartrefreshlayout.finishRefresh();

                    }
                }, 1000);

            }
        });
        /**
         * 加载更多
         */
        donation_smartrefreshlayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page ++;
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        if(state == 0){
                            initRequestMembersArea(1);
                        }else if(state == 1){
                            if(isUpOrDown){
                                initRequestMembersArea(2);
                            }else{
                                initRequestMembersArea(3);
                            }
                        }else if(state == 2){
                            initRequestMembersArea(4);
                        }else if(state == 3){
                            if(isUpOrDown1){
                                initRequestMembersArea(5);
                            }else{
                                initRequestMembersArea(6);
                            }
                        }
                        donation_smartrefreshlayout.finishLoadmore();

                    }
                }, 1000);

            }
        });
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_feature_donation;
    }

    @Override
    public void initPresenter() {
        memberAreaAdaper = new MemberAreaAdaper(getBaseContext(),listItems);
        list_item_view.setAdapter(memberAreaAdaper);



        list_item_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intentOther = new Intent();
                intentOther.setClass(getBaseContext(), ProductsDetailsActivity.class);
                intentOther.putExtra("productId", String.valueOf(listItems.get(i).getId()));
                startActivity(intentOther);

            }
        });
    }

    private void initRequestMembersArea(int type) {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else {
            HttpRetrofitClient.getInstance(getBaseContext())
                    .getUrlRequestMembersAreaTag(Integer.valueOf(tag),type, page, pageNum,
                            new Subscriber<ResponseBody>() {
                                @Override
                                public void onStart() {
                                    super.onStart();
                                    LoadingDialog.showDialogForLoading(MembersAreaTagActivity.this);
                                }

                                @Override
                                public void onCompleted() {
                                    LoadingDialog.cancelDialogForLoading();

                                }

                                @Override
                                public void onError(Throwable e) {

                                }

                                @Override
                                public void onNext(ResponseBody responseBody) {
                                    try {
                                        JSONObject jsonObject = new JSONObject(responseBody.string());
                                        JSONArray jsonRecommend = jsonObject.optJSONArray("data");
                                        Gson gson = new Gson();
                                        List<TestAdapter> forList = new ArrayList<>();
                                        for(int i = 0; i < jsonRecommend.length(); i++){
                                            TestAdapter membersAreasBean = gson.fromJson(jsonRecommend.optString(i).toString(),TestAdapter.class);
                                            forList.add(membersAreasBean);
                                        }
                                        setData(forList);

                                    } catch (Exception e) {
                                        e.printStackTrace();

                                    }

                                }
                            });
        }

    }

    private void setData(List<TestAdapter> foreshowList) {
        Log.d("==",""+listItems.size());
        if(page == 0){
            listItems.clear();
        }
        for (TestAdapter vo : foreshowList) {
            listItems.add(vo);
        }
        memberAreaAdaper.notifyDataSetChanged();

    }
    @OnClick(R.id.ll_line0)
    public void onClickLL_Line0(){
        page =0;
        state= 0;
        items_line0.setVisibility(View.VISIBLE);
        items_line1.setVisibility(View.GONE);
        items_line2.setVisibility(View.GONE);
        items_line3.setVisibility(View.GONE);
        initRequestMembersArea(1);

    }
    @OnClick(R.id.ll_line1)
    public void onClickLL_Line1(){
        page =0;
        state= 1;
        items_line0.setVisibility(View.GONE);
        items_line1.setVisibility(View.VISIBLE);
        items_line2.setVisibility(View.GONE);
        items_line3.setVisibility(View.GONE);
        if(isUpOrDown){
            isUpOrDown = false;
            select_down_or_up_price.setImageResource(R.mipmap.down2x);
            initRequestMembersArea(2);
        }else{
            isUpOrDown = true;
            select_down_or_up_price.setImageResource(R.mipmap.up2x);
            initRequestMembersArea(3);
        }
    }
    @OnClick(R.id.ll_line2)
    public void onClickLL_Line2(){
        page =0;
        state= 2;
        items_line0.setVisibility(View.GONE);
        items_line1.setVisibility(View.GONE);
        items_line2.setVisibility(View.VISIBLE);
        items_line3.setVisibility(View.GONE);
        initRequestMembersArea(4);
    }
    @OnClick(R.id.ll_line3)
    public void onClickLL_Line3(){
        page =0;
        state= 3;
        items_line0.setVisibility(View.GONE);
        items_line1.setVisibility(View.GONE);
        items_line2.setVisibility(View.GONE);
        items_line3.setVisibility(View.VISIBLE);
        if(isUpOrDown1){
            isUpOrDown1 = false;
            select_up_or_down_elecNum.setImageResource(R.mipmap.down2x);
            initRequestMembersArea(5);
        }else{
            isUpOrDown1 = true;
            select_up_or_down_elecNum.setImageResource(R.mipmap.up2x);
            initRequestMembersArea(6);
        }
    }



    @OnClick(R.id.iv_back_page)
    public void onClickFinish(){
        finish();
    }


}
